import {getPointRecord, getPointRecordCount} from "@src/api/points";
import dayjs from "dayjs";
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import base from "@src/store/base";
import router from "@src/router";

Component({
  behaviors: [storeBindingsBehavior, router.routerBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor']
  }],
  data: {
    pointsCountData: null,
    pickerStart: "2018-08-08",
    page: 1,
    pageSize: 10,
    loading: false,
    hasNextPage: true,
    listData: [],
    startDate: null,
    navWChoose: "",
    chooseDateArr: null,
  },

  methods: {
    onLogin() {
      let startDate = dayjs(new Date()).format("YYYY-MM")
      let chooseDateArr = [dayjs(new Date()).format("YYYY"), dayjs(new Date()).format("MM")]
      this.setData({startDate: startDate, chooseDateArr: chooseDateArr})
      this.initLoadingData();
      this.pointsCountReq();
    },
    onPullDownRefresh: function () {
      this.initLoadingData();
      this.pointsCountReq();
    },
    onReachBottom: function () {
      if(this.data.hasNextPage){
        this.setData({page: this.data.page+1})
        this.getListData();
      }
    },
    renderPage() {
      this.pointsCountReq();
    },
    initLoadingData() {
      this.setData({
        page: 1,
        pageSize: 10,
        listData: [],
        hasNextPage: false
      })
      this.getListData()
    },
    //listData接口
    getListData() {
      wx.stopPullDownRefresh();
      let data = {
        time: this.data.startDate,
        type: this.data.navWChoose,
        page: this.data.page,
        pageSize: this.data.pageSize
      };
      this.setData({loading: true})
      getPointRecord(data).then(res => {
        if (res.code === 0) {
          let list = res.data.list||[];
          /*暂停刷新*/
          wx.stopPullDownRefresh();
          let listData = list;
          this.setData({listData: this.data.listData.concat(list), hasNextPage: res.data.hasNextPage, loading:false})
        } else {
          this.setData({loading: false})
        }
      }).catch(() => {
        this.setData({loading: false})
      }).finally(()=>{
        wx.stopPullDownRefresh();
      })
    },
    //积分明细
    pointsCountReq() {
      let data = {
        time: this.data.startDate
      }
      getPointRecordCount(data).then(res => {
        if (res.code === 0) {
          this.setData({pointsCountData: res.data})
        }
      })
    },
    changeStatusGetData(e) {
      this.setData({navWChoose: e.currentTarget.dataset.index})
      this.initLoadingData();
    },
    bindDateChangeStart(event) {
      let startDate = event.detail.value
      if (startDate) {
        let chooseDateArr = startDate.split('-')
        this.setData({chooseDateArr: chooseDateArr, startDate: startDate})
      }
      this.initLoadingData();
      this.pointsCountReq();
    },
    showItemDetail(listItem) {

    }
  }
})
